package name.matthewgreet.example6.action; import java.math.BigDecimal; import java.util.List; import javax.servlet.http.HttpSession; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; import name.matthewgreet.example6.dto.GameDTO; import name.matthewgreet.example6.dto.UserRatingDTO; import name.matthewgreet.example6.form.UserRatingRatingForm; import name.matthewgreet.example6.model.Model; import name.matthewgreet.example6.model.UserRatingPK; import name.matthewgreet.example6.model.UserRatingTable; import name.matthewgreet.example6.util.GameCompositeCache; import name.matthewgreet.strutscommons.action.AbstractFormDrivenActionSupport; import name.matthewgreet.strutscommons.util.ListCache; import name.matthewgreet.strutscommons.util.ListFinder; /** * Finds user ratings for a game by its rating band. Game ratings can have one decimal place and ratings are in the * same band if they have the same integer part, from 0 to 10. */ @InterceptorRef("FormDrivenStack") @Results({ @Result(name=ActionSupport.INPUT, type="redirectAction", params = {"namespace", "/", "actionName", "ViewUserRatingList"}), @Result(name=ActionSupport.SUCCESS, type="redirectAction", params = {"namespace", "/", "actionName", "ViewUserRatingList"}) }) public class FindUserRatingListByRatingAction extends AbstractFormDrivenActionSupport<UserRatingRatingForm> { public static class UserRatingByRatingListFinder implements ListFinder<GameDTO,UserRatingDTO> { private static final long serialVersionUID = 9200935020719468634L; private Integer rating; public UserRatingByRatingListFinder(Integer rating) { this.rating = rating; } @Override public List<UserRatingDTO> getList(GameDTO selectedMaster) throws Exception { UserRatingTable userRatingTable; BigDecimal rangeStart, rangeEnd; userRatingTable = Model.getInstance().getUserRatingTable(); if (rating != null) { rangeStart = new BigDecimal(rating); rangeEnd = rangeStart.add(new BigDecimal("0.9")); return userRatingTable.findByGameIdAndRatingOrdered(selectedMaster.getId(), rangeStart, rangeEnd); } else { return userRatingTable.findByGameIdOrdered(selectedMaster.getId()); } } public Integer getRating() { return rating; } } private static final long serialVersionUID = -7979376337555804889L; @Action("/FindUserRatingListByRating") @Override public String execute() throws Exception { UserRatingRatingForm form; ListCache<GameDTO,UserRatingPK,UserRatingDTO> listCache; HttpSession session; form = getModel(); session = getBrowserTabSession(); listCache = GameCompositeCache.getInstance(session).getSlaveListCache2(); listCache.setBaseRecordListFinder(new UserRatingByRatingListFinder(form.getParsedRating())); return SUCCESS; } @Override protected UserRatingRatingForm makeForm() { return new UserRatingRatingForm(); } }